<?php
/** 
 * Login_model
 * This class handles model work necessary for logging in user
 * 
 * @author Ray Nowell
 *	
 */ 
class Login_model extends CI_Model {

	function Login_model()
	{
		parent::__construct();
                $this->load->library('encrypt');
	}
	function update_login($password) {
        $encrypted_password = $this->encrypt->encode($password);
        $member_id = $this->session->userdata('member_id');
        
        $data = array(	'password'  =>	$encrypted_password);
        
        $this->db->where('member_id',$member_id);
        
        $this->db->update('member',$data);
		return ;
        
    }
	function check_login($password) {
        $ret_val = false;

        $member_id = $this->session->userdata('member_id');
        
        $query = $this->db->limit(1)->get_where('member', array('member_id' => $member_id));
        
        $row = $query->row();
        $decoded_pw = $this->encrypt->decode($row->password);  
                
        if ($password != $decoded_pw) 
            return false;
                                
        return true;
    }
	function login_user($username, $password)
	{
            $username = trim($username);
            $encrypted_password = $this->encrypt->encode($password);
            
            $query = $this->db->limit(1)->get_where('member', array('email' => $username));

            $ret_val= '';
            if ($query->num_rows() === 0)
            {
                return array('success'  =>  false,
                                 'message'  =>  'Username not found');
            }
            else
            {
              
                $row = $query->row();
                $decoded_pw = $this->encrypt->decode($row->password);  
                
                if ($password != $decoded_pw)
                {
                  return array('success'  =>  false,
                                  'message'  =>  'Password incorrect');                
                }
                
                if ($row->active == 0) 
                {
                    $ret_val = array('success'  =>  false,
                                     'member_id' => $row->member_id,
                                     'first_name' => $row->first_name,
                                     'message'  =>  'User is not active');                
                }
                else
                {
                    $ret_val = array('success'  =>  true,
                                     'member_id' => $row->member_id,
                                     'first_name' => $row->first_name,
                                     'is_admin' =>  $row->site_admin,
                                     'message'  =>  '');                
                }
            }
            return $ret_val;
	}
}

/* End of file login_model.php */
/* Location: ./system/application/models/login_model.php */